#include "camera.h"

OrthographicCamera::OrthographicCamera(Vec3f center, Vec3f direction, Vec3f up, float size)
{
	m_center = center;
	m_direction = direction;
	m_up = up;
	m_direction.Normalize();
	Vec3f::Cross3(m_horizontal, m_direction, m_up);
	m_horizontal.Normalize();
	Vec3f::Cross3(m_up, m_horizontal, m_direction);
	m_up.Normalize();
	m_size = size;
}

Ray OrthographicCamera::generateRay(float fx, float fy)
{
	Vec3f orig = m_center + m_up*((fy-0.5f)*m_size)
		+ m_horizontal*((fx-0.5f)*m_size);
	return Ray(m_direction, orig);
}

void OrthographicCamera::debugMessage() 
{
	printf("OrthographicCamera{\n");
	printf("\tcenter=("); m_center.Write(); printf(")\n");
	printf("\tdirection=("); m_direction.Write(); printf(")\n");
	printf("\tup=("); m_up.Write(); printf(")\n");
	printf("}//end OrthographicCamera\n\n");
}

PerspectiveCamera::PerspectiveCamera(Vec3f center, Vec3f direction, Vec3f up, float angle_radians,float HSW)
{
	m_center = center;
	m_direction = direction;
	m_up = up;
	m_direction.Normalize();
	Vec3f::Cross3(m_horizontal, m_direction, m_up);
	m_horizontal.Normalize();
	Vec3f::Cross3(m_up, m_horizontal, m_direction);
	m_up.Normalize();

	m_angle = angle_radians;
	m_width = tan(m_angle/2) * 2;
	m_height = m_width * HSW;
	m_center = center + direction;
}

Ray PerspectiveCamera::generateRay(float fx, float fy) 
{
	Vec3f direction = m_direction + m_up*((fy-0.5f)*m_height) + 
		m_horizontal*((fx-0.5f)*m_width);
	direction.Normalize();
	return Ray(direction,m_center);
}

void PerspectiveCamera::debugMessage() 
{
	printf("PerspectiveCamera{\n\tangle=(%.3f),size=(%.3f)\n",m_angle,m_width);
	printf("\tcenter=("); m_center.Write(); printf(")\n");
	printf("\tdirection=("); m_direction.Write(); printf(")\n");
	printf("\tup=("); m_up.Write(); printf(")\n");
	printf("}//end PerspectiveCamera\n\n");
}
